#include "gd32f30x.h"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "DevicesCAN.h"
#include "microMemory.h"
#include "saeJ1939.h"


void *pvJ1939Malloc(uint32_t uiSize)
{
    return pvMrMemoryMalloc(uiSize);
}

void vJ1939Free(void *pvMem)
{
    return vMrMemoryFree(pvMem);
}

int8_t cJ1939PduSend(J1939PduType *ptypeJ1939Pdu)
{
    uint32_t uiID, i;

    printf("cJ1939PduSend    P: %d, DP: %d, PF: %02X, PS: %02X, SA: %02X, length: %d, datas:",
                                                                                            ptypeJ1939Pdu->P,
                                                                                            ptypeJ1939Pdu->DP,
                                                                                            ptypeJ1939Pdu->PF,
                                                                                            ptypeJ1939Pdu->PS,
                                                                                            ptypeJ1939Pdu->SA,
                                                                                            ptypeJ1939Pdu->length);
    for(i = 0; i < ptypeJ1939Pdu->length; ++i)
    {
        printf(" %02X", ptypeJ1939Pdu->datas[i]);
    }
    printf("\r\n");

    uiID  = ((uint32_t)ptypeJ1939Pdu->P  << 26);
    uiID |= ((uint32_t)ptypeJ1939Pdu->DP << 24);
    uiID |= ((uint32_t)ptypeJ1939Pdu->PF << 16);
    uiID |= ((uint32_t)ptypeJ1939Pdu->PS << 8);
    uiID |= ptypeJ1939Pdu->SA;

    return cCanxSendDatas(CAN0, uiID, ptypeJ1939Pdu->datas, ptypeJ1939Pdu->length);
}
